﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BySoft.Web.Utility;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Server.Common.Entity;
using BySoft.Server.Business;
using BySoft.Server.Common.Enum;
using OneSoft.EntityCore;
using System.Web;
using System.Web.UI.HtmlControls;
using ZCZP.Web.Site;

namespace BySoft.Web.Site.OutSource
{
    public partial class OutRepairView : AuthBasePage
    {
        #region 属性定义
        private string m_strsequenceName = "BsOutrepair" + DateTime.Now.ToString("yyyyMMdd");
        private string m_strTbl = "BS_OutRepair";
        private string m_strMainKey = "BillID";

        private DataTable DtOutRepairData
        {
            get
            {
                if (null == Session["OutRepairData"])
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["OutRepairData"];
                }
            }
            set
            {
                Session["OutRepairData"] = value;
            }
        }

        private DataTable CreateDT()
        {

            return BsOutrepair.CreateDT();
        }
        #endregion

        #region 初始化及绑定控件
        private void InitData()
        {
            DtOutRepairData = null;
            SetControlAttr(new BsOutrepairEntity(), tBase);
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            InitHead(strCode);
            InitBody(strCode);
        }

        private void InitHead(string strCode)
        {
            //DataTable dt = BsOutrepair.SelectHeadData(strCode);
            //DBtoControlHead(this.Page, dt, tBase, @"../OutOrder/OutRepairList.aspx");
            //DataRow row = dt.Rows[0];
            //hidOutBackMatID.Value = CommonMethod.GetString(row["OutBackMatID"]);
            //hdCode.Value = CommonMethod.GetString(row["BillID"]);
            
        }

        private void InitBody(string strCode)
        {
            DataTable dt = BsOutrepair.SelectHeadData(strCode);
            if (dt.Rows.Count <=0)
            {
                return;
            }
            DataRow dr = dt.Rows[0];

            hdCode.Value = strCode;
            lblBillID.Text = strCode;
            lblBillDate.Text = CommonMethod.GetString(dr["BillDate"]);
            hidOutBackMatID.Value = CommonMethod.GetString(dr["OutBackMatID"]);

               string sendID=hidOutBackMatID.Value;
                string materialID = dr["MaterialID"].ToString();
                DataTable dtCanBack = BsOutrepair.SelectMatNeedRepairNum(strCode,sendID,materialID);
                if (dtCanBack.Rows.Count > 0)
                {

                DataRow dr0 = dtCanBack.Rows[0];
             
                dr["CanBackNum"] = dr0["CanBackNum"];
                }
                else
                {
                  
                    dr["CanBackNum"] = dr["Nums"];
                }


                txtNums.Text = CommonMethod.GetString(dr["Nums"]);

                FillHead( dr);

            DtOutRepairData = dt.Copy();
            BindData();
        }
        #endregion

        #region 控件事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
                InitData();
            }
            Master.ValidationGroup = "gSubmit";
                 }



       

        private void BindData()
        {

            UIHelper.RunScript(this.UpdatePanel1, "iframeAutoFit();");
        }



       
     

        private void FillHead( DataRow dr)
        {
            txtOutBackMatID.Text = CommonMethod.GetString(dr["OutBackMatID"]);
            txtWorkOrderNo.Text = CommonMethod.GetString(dr["WorkOrderNo"]);
            txtScheduleID.Text = CommonMethod.GetString(dr["ScheduleID"]);
            hidSupplierID.Value = CommonMethod.GetString(dr["SupplierID"]);
            txtSupplierName.Text = CommonMethod.GetString(dr["SupplierName"]);
          //  txtOutBackMatID.Text = strOutBackMatID;
            //txtBackProcessID.Text = CommonMethod.GetString(dr["BackProcessID"]);
            txtMaterialID.Text = CommonMethod.GetString(dr["MaterialID"]);

            //txtOutProcessID.Text = CommonMethod.GetString(dr["OutProcessID"]);
            txtMaterialName.Text = CommonMethod.GetString(dr["MaterialName"]);
            txtSpecifications.Text = CommonMethod.GetString(dr["Specifications"]);
            txtUnit.Text = CommonMethod.GetString(dr["Unit"]);
            txtCanBackNum.Text = CommonMethod.GetString(dr["CanBackNum"]);

            string strDate = CommonMethod.GetString(dr["ExpectBackDate"]);
            if (!string.IsNullOrEmpty(strDate))
            {
                DateTime date = DateTime.Parse(strDate);

                cpExpectBackDate.Value = date.ToString("yyyy-MM-dd");

            }
           
        }


        #endregion



        #region 本地方法
        private bool ValidateData()
        {
            string strMention = "";


            //if (ClsComm.CheckExist(m_strTbl, m_strMainKey, lblBillID.Text.Trim(), true))
            //{
            //    strMention = "委外件退料编号已经存在！<br />";
            //}

            decimal sum = CommonMethod.ConvertToDecimal(txtNums.Text, 2);

            if (sum <= 0)
            {
                strMention = "回厂数量不可为空！<br />";
            }
            if (string.IsNullOrEmpty(strMention))
            {
                return true;
            }
            else
            {
                UIHelper.AlertMessage(this.Page, strMention);
                return false;
            }
        }
        #endregion
    }
}

